package ru.javadevelop.vegetarian.model;

import ru.javadevelop.vegetarian.common.DB;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.UUID;

public class Category {
    public int id;
    public int parentId;
    public String name;

    public Category(int id, int parentId, String name)
    {
        this.id = id;
        this.parentId = parentId;
        this.name = name;
    }

    //********************************************************************************************************
    public int save() throws SQLException {
        boolean exists = this.id != 0 && DB.INSTANCE.executeSql(new DB.Handler<Boolean>() {
            @Override
            public Boolean  execute(Statement stmt) throws SQLException {
                ResultSet resultSet = stmt.getResultSet();
                return resultSet.next();
            }
        }, "SELECT * FROM Category WHERE id=?", this.id);
        // Если запись существовала, — обновляем её  (проверяется по id)
        // у вновь создаваемых записей id=0

        if  (exists)
        {
            DB.INSTANCE.executeSql("UPDATE Category SET parentId = ?, name = ? WHERE id = ?",
                                    this.parentId, this.name, this.id);
            return this.id;
        }
        else
        {
            DB.INSTANCE.executeSql("INSERT INTO Category (id, parentId, name) VALUES(?,?,?)",
                    this.id, this.parentId, this.name);
            return this.id;
        }
    }
    //********************************************************************************************************
    public static Category getById(int id) {
        return DB.INSTANCE.executeSql(new DB.Handler<Category>() {
            @Override
            public Category execute(Statement stmt) throws SQLException {
                ResultSet resultSet = stmt.getResultSet();
                resultSet.next();
                return  new Category(
                        resultSet.getInt("id"),
                        resultSet.getInt("parentId"),
                        resultSet.getString("name"));
            }
        }, "SELECT * FROM category WHERE id=?", id);
    }

    //********************************************************************************************************
    public static ArrayList<Category> getAll()
    {
        return DB.INSTANCE.executeSql(new DB.Handler<ArrayList<Category>>() {
            @Override
            public ArrayList<Category> execute(Statement stmt) throws SQLException {
                ArrayList<Category> result = new ArrayList<Category>();
                ResultSet resultSet = stmt.getResultSet();
                while (resultSet.next())
                {
                    result.add(new Category(
                            resultSet.getInt("id"),
                            resultSet.getInt("parentId"),
                            resultSet.getString("name")));
                }
                return result;

            }
        }, "SELECT * FROM category");
    }
    //********************************************************************************************************
    public String toString()
    {
        return name;
    }

    public void delete() {
            DB.INSTANCE.executeSql("DELETE FROM Category WHERE id=?", this.id);
    }
    //********************************************************************************************************
    @Override
    public boolean equals(Object category)
    {

        return ( (this.id==((Category)category).id) &&
                (this.name.equals(((Category)category).name))&&
                (this.parentId==((Category)category).parentId) );
    }
    //********************************************************************************************************
}
